package TRSS
import (
	"fmt"
	"io/ioutil"
	"gopkg.in/yaml.v2"
	"time"
	"strings"
	"strconv"
	"os"
	"github.com/olekukonko/tablewriter"
)

func TRSS_show_info(GoBoxPath,BotPath string) {
	// 读取并解析config1.yaml文件
	os.Chdir(GoBoxPath)
	configPath, err := getConfigPath(GoBoxPath,BotPath)
	if err != nil {
		fmt.Println(Red,"获取配置文件失败", err, Reset)
		// time.Sleep(2 * time.Second)
	}
	//判断是否存在，不存在则返回
	//打印当前目录
	fmt.Println("配置文件:", configPath)
	if _, err := os.Stat(configPath); os.IsNotExist(err) {
		fmt.Println(Red,"配置文件不存在,请完成初始化...", Reset)
		time.Sleep(2 * time.Second)
		return
	}
	data, err := ioutil.ReadFile(configPath)
	if err != nil {
		fmt.Println(Red, "读取配置文件失败", err, Reset)
		time.Sleep(2 * time.Second)
		return
	}
	var config token
	err = yaml.Unmarshal(data, &config)
	if err != nil {
		fmt.Println(Red, "解析配置文件失败", err, Reset)
		time.Sleep(2 * time.Second)
		return
	}
	if len(config.Token) == 0 {
		fmt.Println(Red, "没有配置账户...", Reset)
		time.Sleep(2 * time.Second)
		return
	}

	// 输出账户信息
	table1 := tablewriter.NewWriter(os.Stdout)
	// 设置表格列宽
	table1.SetHeader([]string{"Appid", "沙盒", "域", "Token"})
	for _, t := range config.Token {
		fields := strings.Split(t, ":")
		if len(fields) == 4 {
			Appid := fields[2]
			sandbox, _ := strconv.Atoi(fields[0])
			domain, _ := strconv.Atoi(fields[1])
			token := fields[3]
			// 对输出进行优化
			var sandboxStr string
			if sandbox == 0 {
				sandboxStr = "关闭"
			} else {
				sandboxStr = "开启"
			}
			var domainStr string
			if domain == 0 {
				domainStr = "公域"
			} else {
				domainStr = "私域"
			}
			table1.Append([]string{Appid, sandboxStr, domainStr, token})
		}
	}
	//打印表格标题
	fmt.Println("Bot账户信息:")
	table1.SetColMinWidth(0, 15)
	table1.SetColMinWidth(1, 4)
	table1.SetColMinWidth(2, 4)
	table1.SetColMinWidth(3, 40)
	//内容左
	table1.SetAlignment(tablewriter.ALIGN_CENTER)

	
	table1.Render()
	//表格
	// 读取并解析config2.yaml文件
	configPath2 := BotPath + "/config/config/other.yaml"
	fmt.Println("配置文件:", configPath2)
	data, err = ioutil.ReadFile(configPath2)
	if err != nil {
		fmt.Println(Red, "读取配置文件失败", err, Reset)
		time.Sleep(2 * time.Second)
		return
	}
	var AdminConfig Admin
	err = yaml.Unmarshal(data, &AdminConfig)
	if err != nil {
		fmt.Println(Red, "解析配置文件失败", err, Reset)
		return
	}

	// 输出主人QQ
	fmt.Println("主人QQ/频道:")
// 创建表格对象并添加数据
	table2 := tablewriter.NewWriter(os.Stdout)
	table2.SetHeader([]string{"序号id", "主人QQ/频道"})
	for i, v := range AdminConfig.MasterQQ {
		if v == "stdin" {
			continue
		}
		table2.Append([]string{fmt.Sprintf("%d", i+1), v})
	}
	table2.SetColMinWidth(0, 19)
	table2.SetColMinWidth(1, 50)
	//内容左
	table2.SetAlignment(tablewriter.ALIGN_CENTER)
	table2.Render()
	// 输出多账号主人
	fmt.Println("多账号主人:")
	table3 := tablewriter.NewWriter(os.Stdout)
	table3.SetHeader([]string{"Bot机器人", "主人账号"})
	for _, v := range AdminConfig.Master {
		fields := strings.Split(v, ":")
		if fields[1] == "stdin" {
			continue
		}
		table3.Append([]string{fields[0], fields[1]})
	}

	table3.SetColMinWidth(0, 19)
	table3.SetColMinWidth(1, 50)
	//内容左
	table3.SetAlignment(tablewriter.ALIGN_CENTER)
	table3.Render()
	// 按回车键继续
	fmt.Printf("\n按回车键继续")
	fmt.Scanln()
}
